function e(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

function t(e, t, n) {
    return t in e ? Object.defineProperty(e, t, {
        value: n,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : e[t] = n, e;
}

Object.defineProperty(exports, "__esModule", {
    value: !0
});

var n = e(require("../../utils/util.js")), o = e(require("../../utils/toptip.js")), d = e(require("../../utils/event-emitter.js")), i = e(require("../../utils/api"));

exports.default = {
    addTo: function(e) {
        var a = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : "$$mv";
        e.setData(t({}, a, {
            form: {
                mobile: "",
                verifycode: ""
            },
            enabledSendCode: !1,
            sendCodeButtonText: "验证手机"
        }));
        Object.assign(e, {
            countdown: 0,
            $$mvInputMobile: function(e) {
                var n = e.detail.value.trim();
                this.setData(t({}, a + ".form.mobile", n)), this.$$mvUpdateEnabledSendCode(), d.default.emit("$$mv.mobileChange", n);
            },
            $$mvInputVCode: function(e) {
                var n = e.detail.value.trim();
                this.setData(t({}, a + ".form.verifycode", n)), d.default.emit("$$mv.verifyCodeChange", n);
            },
            $$mvUpdateEnabledSendCode: function() {
                var e = this.data[a];
                this.setData(t({}, a + ".enabledSendCode", n.default.isMobile(e.form.mobile) && 0 === this.countdown));
            },
            $$mvSendCode: function() {
                var e = this, t = this.data[a].form.mobile, n = this.$$mvStartCountdown();
                i.default.users.sendcode({
                    source: "weApp",
                    mobile: t
                }).then(function(e) {
                    if (0 !== e.code || !e.data.issend) throw new Error(e.message);
                }).catch(function(t) {
                    n(), o.default.show(e, "短信发送失败，请重试"), e.$$mvUpdateSendCodeStatusByCountDown(), d.default.emit("$$mv.sendCodeFail");
                });
            },
            $$mvUpdateSendCodeStatusByCountDown: function() {
                var e = 0 == +this.countdown ? "重新验证" : this.countdown + "S";
                this.setData(t({}, a + ".sendCodeButtonText", e)), 0 !== this.countdown && 60 !== this.countdown || this.$$mvUpdateEnabledSendCode();
            },
            $$mvStartCountdown: function() {
                var e = this, t = void 0;
                return this.countdown = 60, this.$$mvUpdateSendCodeStatusByCountDown(), t = setInterval(function() {
                    e.countdown--, e.$$mvUpdateSendCodeStatusByCountDown(), 0 === e.countdown && clearInterval(t);
                }, 1e3), function() {
                    e.countdown = 0, clearInterval(t);
                };
            }
        });
    }
};